/**
 *	@file	pm_tel.c
 *	@brief	ポケモン　電話システム
 *	@author	Miyuki Iwasawa
 *	@date	07.10.24
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/zonedata.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"

#define __PM_TEL_H_GLOBAL
#include "pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "play/telseq_play.h"

#define WORDSET_TEL_BUFLEN	((18*2)+EOM_SIZE)
#define WORDSET_TEL_BNUM	(16)
#define BUFLEN_TEL_MSG		((18*30)*2+EOM_SIZE)
#define BUFLEN_TEL_NAME		(18*2+EOM_SIZE)

extern const TELD_SEQ_SIMPLE PMTelDatTbl_Simple[];

/**
 *	@brief	電話データのコントロールマネージャを作成
 *
 *	@param	heapID	ワークを確保するheapID
 */
PMTEL_DATA_MAN* PMTelData_Init(PMTEL_DATA_PARAM* prm)
{
	PMTEL_DATA_MAN* man;
	int	i;

	man = sys_AllocMemory(prm->heapID,sizeof(PMTEL_DATA_MAN));
	MI_CpuClear8(man,sizeof(PMTEL_DATA_MAN));

	man->heapID = prm->heapID;
	man->pBookMan = PMTelBookData_AllocAll(prm->heapID);
	man->book = man->pBookMan->data;	//&(pmtel_book_data[0]);

	man->tel_sys_work = prm->tel_sys_work;
	man->pKeymode = prm->pKeymode;
	man->act_btn = prm->act_btn;

	man->bgl = prm->bgl;
	man->win_msg = prm->win_msg;
	man->win_name = prm->win_name;
	man->palAnm = prm->palAnm;

	man->save = prm->save;
	man->tel_sv = prm->tel_save;
	man->my_sv	= prm->my_save;
	man->ev_sv	= prm->ev_save;
	man->time	= prm->time;

	man->zone_id = prm->zone_id;
	man->field_zone = prm->field_zone;
	man->msg_spd = prm->msg_spd;


	man->my_sex = MyStatus_GetMySex(man->my_sv);

	man->msgMan	= MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,NARC_msg_pg_tel_dat,man->heapID);
	man->m_gt	= MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,NARC_msg_tel_greeting_dat,man->heapID);

	//ワードセット領域作成
	man->wset = WORDSET_CreateEx(WORDSET_TEL_BNUM,WORDSET_TEL_BUFLEN,man->heapID);
	//1メッセージ分のテンポラリバッファ生成
	man->str_tmp = STRBUF_Create(BUFLEN_TEL_MSG,man->heapID);
	man->str_talk = STRBUF_Create(BUFLEN_TEL_MSG,man->heapID);
	man->str_name = STRBUF_Create(BUFLEN_PERSON_NAME*2,man->heapID);
	man->str_trtype = STRBUF_Create(BUFLEN_TRAINER_TYPE_NAME,man->heapID);
	man->str_tr_tmp = MSGMAN_AllocString(man->msgMan,msg_trtype_tmp);

	for(i = 0;i < TEL_CALL_SE_NUM;i++){
		man->str_call_s[i] = MSGMAN_AllocString(man->msgMan,msg_com_talk_dot01+i);
		man->str_call_e[i] = MSGMAN_AllocString(man->msgMan,msg_com_talk_end01+i);
	}
	return man;
}

/**
 *	@brief	電話データコントロールマネージャの破棄
 *
 */
void PMTelData_Release(PMTEL_DATA_MAN* man)
{
	int i;
	for(i = 0;i < TEL_CALL_SE_NUM;i++){
		STRBUF_Delete(man->str_call_s[i]);
		STRBUF_Delete(man->str_call_e[i]);
	}
	STRBUF_Delete(man->str_tr_tmp);
	STRBUF_Delete(man->str_trtype);
	STRBUF_Delete(man->str_name);
	STRBUF_Delete(man->str_talk);
	STRBUF_Delete(man->str_tmp);
	WORDSET_Delete(man->wset);

	MSGMAN_Delete(man->m_gt);
	MSGMAN_Delete(man->msgMan);

	PMTelBookData_Free(man->pBookMan);
	MI_CpuClear8(man,sizeof(PMTEL_DATA_MAN));
	sys_FreeMemoryEz(man);
}

/**
 *	@brief	電話　指定番号の人物名を取得
 *
 *	＊システム内のバッファに文字列を展開し、そのポインタを返す
 */
const STRBUF* PMTelData_GetNameStr(PMTEL_DATA_MAN* man,PMTEL_NUMBER_ID id)
{
	MSGDATA_MANAGER* msg;

	if(id >= PMTEL_NUMBER_MAX){
		id = 0;
	}
	msg	= MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,PMTelData_GetPersonalMsgArcID(id),man->heapID);
	MSGMAN_GetString(msg,0,man->str_name);
	MSGMAN_Delete(msg);
	return man->str_name;
}

/**
 *	@brief	電話　指定番号のトレーナータイプ名を取得
 *
 *	＊システム内のバッファに文字列を展開し、そのポインタを返す
 */
const STRBUF* PMTelData_GetTrtypeStr(PMTEL_DATA_MAN* man,PMTEL_NUMBER_ID id)
{
	u8 type;

	if(id >= PMTEL_NUMBER_MAX){
		id = 0;
	}
	type = man->book[id].tr_type;
	if(type == TEL_TRTYPE_NOCODE){
		STRBUF_Clear(man->str_trtype);
	}else if(type >= TEL_TRTYPE_SPCODE){
		MSGMAN_GetString(man->msgMan,(type-TEL_TRTYPE_SPCODE)+msg_tr_title_01,man->str_trtype);
	}else{
		WORDSET_RegisterTrTypeName( man->wset,0,man->book[id].tr_type);
		WORDSET_ExpandStr(man->wset,man->str_trtype,man->str_tr_tmp);
	}
	return man->str_trtype;
}

/**
 *	@brief	電話　ターゲットデータ取得
 */
BOOL PMTelData_TalkTargetSet(PMTEL_DATA_MAN* man,PMTEL_NUMBER_ID id,
		const BOOL is_receive,const u8 ev_type,const u8 ev_id)
{
	PMTEL_TALK_WORK* wk = &man->twk;
	MI_CpuClear8(wk,sizeof(PMTEL_TALK_WORK));

	//IDが不正なら圏外にしてしまう
	if((id >= PMTEL_NUMBER_MAX)){
		GF_ASSERT_MSG(0,"不正な電話番号 %d\n",id);
		wk->main_seq = 0xFF;
		return FALSE;
	}
	//規定ゾーンでは圏外処理
	if(is_receive){
		if(	!ZoneData_GetEnableTelReceiveFlag(man->zone_id)){
			wk->main_seq = 0xFF;
			return FALSE;
		}
	}else{
		if(	!ZoneData_GetEnableTelCallFlag(man->zone_id)){
			wk->main_seq = 0xFF;
			return FALSE;
		}
	}

	wk->id = id;
	wk->pbk = (PMTEL_BOOK*)&man->book[id];
	wk->is_receive = is_receive;
	wk->ev_type = ev_type;
	wk->ev_id = ev_id;

	wk->main_seq = wk->is_receive;

	//現在の年月日、時刻コードを取得
	GF_RTC_GetDateTime(&wk->rtc_date, &wk->rtc_time);
	wk->time_zone = GF_RTC_ConvertHourToGSTimeZone(wk->rtc_time.hour);

	wk->seq_code = 0;
	wk->seq_no = 0;

	return TRUE;
}

/**
 *	@brief	電話　会話内容抽選
 */
void PMTelData_TalkDataSet(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK* wk = &man->twk;

	//ウィンドウ初期化
	GF_BGL_BmpWinDataFill(man->win_msg,0);
	GF_BGL_BmpWinOnVReq(man->win_msg);

	//抽選タイプによって、会話内容を決定する
	switch(wk->pbk->play_list){
	case TEL_PLAYL_REVENGE:
		wk->seq_no = PMTelPlayL_Revenge(man,wk);
		break;
	case TEL_PLAYL_MAMA:
		wk->seq_no = PMTelPlayL_Mama(man,wk);
		break;
	case TEL_PLAYL_UTSUGI:
		wk->seq_no = PMTelPlayL_Utsugi(man,wk);
		break;
	case TEL_PLAYL_OOKIDO:
		wk->seq_no = PMTelPlayL_Ookido(man,wk);
		break;
	case TEL_PLAYL_CYCLE:
		wk->seq_no = PMTelPlayL_Cycle(man,wk);
		break;
	case TEL_PLAYL_MASAKI:
		wk->seq_no = PMTelPlayL_Masaki(man,wk);
		break;
	case TEL_PLAYL_GRANDMA:
		wk->seq_no = PMTelPlayL_Grandma(man,wk);
		break;
	case TEL_PLAYL_GRANDPA:
		wk->seq_no = PMTelPlayL_Grandpa(man,wk);
		break;
	case TEL_PLAYL_AOI:
		wk->seq_no = PMTelPlayL_Aoi(man,wk);
		break;
	case TEL_PLAYL_SUPPORT:
		wk->seq_no = PMTelPlayL_Support(man,wk);
		break;
	case TEL_PLAYL_GANTETSU:
		wk->seq_no = PMTelPlayL_Gantetsu(man,wk);
		break;
	case TEL_PLAYL_LEADER:
		wk->seq_no = PMTelPlayL_Leader(man,wk);
		break;
	case TEL_PLAYL_TAKENORI:
		wk->seq_no = PMTelPlayL_Takenori(man,wk);
		break;
	case TEL_PLAYL_SAFARI:
		wk->seq_no = PMTelPlayL_Safari(man,wk);
		break;
	case TEL_PLAYL_MIC:
		wk->seq_no = PMTelPlayL_Mic(man,wk);
		break;
	case TEL_PLAYL_NORMAL:
	default:
		wk->seq_code = TEL_SEQ_SIMPLE;
		wk->seq_no = 0;
		break;
	}
	if(wk->seq_code <= TEL_SEQ_REV_RND11){
		(const TELD_SEQ_SIMPLE*)wk->data = &(PMTelDatTbl_Simple[wk->seq_no]);
	}
}
